如何將 Wagtail 'admin' 菜單添加到自定義模板? (How to add Wagtail 'admin' menu to custom templates?)


問題描述

如何將 Wagtail 'admin' 菜單添加到自定義模板? (How to add Wagtail 'admin' menu to custom templates?)

在源自 Wagtail 頁面模型的模板上,右下角有一個小 Wagtail 圖標/菜單。這提供了一種快速編輯頁面和/或跳轉到 Wagtail 管理員的方法。但是,此菜單不會出現在不是從 Wagtail 頁面模型派生的自定義視圖模板上。

我如何告訴 Wagtail 在我的前端模板上顯示小菜單,以便頁面具有一致的導航?


參考解法

方法 1:

The standard Wagtail user bar is rendered by placing in your template:

{% load wagtailuserbar %}

{% wagtailuserbar 'top‑left' %}

I typically just put the above in base.html. The 'top‑left' designation, of course, tells the template tag where to render the user bar.   Reference

However, the wagtailuserbar is only rendered for Wagtail pages. What you want to do is render the user bar with only the Go to Wagtail Admin option (because no other options would be relevant). Therefore, you could create your own template tag and place it in base.html beside the wagtailuserbar. You would set it up so that it renders if there IS NO page in the context (the wagtailuserbar template tag checks to make sure there IS a page in the context). To create your tag, just start with the code from wagtailuserbar.py and modify it to create a template tag called wagtailuserbar_admin_only (untested):

from django import template
from django.template.loader import render_to_string
from wagtail.admin.templatetags.wagtailuserbar import get_page_instance
from wagtail.admin.userbar import (AdminItem)

@register.simple_tag(takes_context=True)
def wagtailuserbar_admin_only(context, position='bottom‑right'):
    # Find request object
    try:
        request = context['request']
    except KeyError:
        return ''

    # Don't render without a user because we can't check their permissions
    try:
        user = request.user
    except AttributeError:
        return ''

    # Don't render if user doesn't have permission to access the admin area
    if not user.has_perm('wagtailadmin.access_admin'):
        return ''

    # Only render if the context does NOT contain a variable referencing a saved page
    page = get_page_instance(context)
    if page:
        return ''

    # Render the items
    rendered_items = [AdminItem()]

    # Render the userbar items
    return render_to_string('wagtailadmin/userbar/base.html', {
        'request': request,
        'items': rendered_items,
        'position': position,
    })

Then, to use in templates, place in base.html:

{% load wagtailuserbar_admin_only %}

{% wagtailuserbar_admin_only 'top‑left' %}

(by Brylie Christopher OxleyDan Swain)

參考文件

  1. How to add Wagtail 'admin' menu to custom templates? (CC BY‑SA 2.5/3.0/4.0)

#wagtail #django-views #Django #django-templates






相關問題

Wagtail Cms 是否支持 Google 登錄和用戶登錄添加會話 (Does Wagtail Cms support Google login and user login add session to)

Wagtail Django-form編輯現有對象 (Wagtail Django-form edit existing object)

如何將 Wagtail 'admin' 菜單添加到自定義模板? (How to add Wagtail 'admin' menu to custom templates?)

django.db.utils.OperationalError:外鍵不匹配 - “project_projectpage”引用“auth_user” (django.db.utils.OperationalError: foreign key mismatch - "project_projectpage" referencing "auth_user")

如何使用 Wagtail 鉤子在 Wagtail 中生成自定義鏈接 (How to generate a custom link in Wagtail using Wagtail hooks)

Wagtail:如何設置單元測試以進行簡單的頁面編輯? (Wagtail: How to setup up unittest for simple page edit?)

如何修復錯誤“str”對像沒有屬性“relative_url” (How to fix error 'str' object has no attribute 'relative_url')

如何將帖子從 Wordpress 導入 Wagtail 2(Draftail 編輯器),包括圖像? (How to import posts from Wordpress to Wagtail 2 (Draftail editor) including images?)

如何用外鍵鏈接兩種形式(wagtail 形式和 django 形式)? (How to link two forms (wagtail form and django form) with a foreign key?)

為什麼 RichText 不能在 wagtail 管理員中為帖子工作?這是發生的事情的類型:<h2>嘗試 post.content|richtext</h2> (Why is RichText not working in wagtail admin for posts? This is the type of thing that happens: <h2>Trying post.content|richtext</h2>)

Windows 10 上 wagtail 的客戶端文件夾在哪裡 (Where is the client folder of wagtail on windows 10)

過濾從 Wagtail 核心頁面導入的多個模型的自定義字段 (Filter on custom field across multiple models that import from Wagtail core Page)







留言討論